package cn.zxcoder.config;

import cn.zxcoder.Tools.TokenUtil;
import cn.zxcoder.Tools.ToolUtil;
import io.swagger.annotations.Api;
import org.apache.catalina.connector.Response;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResultUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;


@Component
@Api(tags = "登录验证的拦截器")
public class LoginInterceptor implements HandlerInterceptor {


    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token == null) {
            token = request.getParameter("token");
        }
        System.out.println(token);
//        String username = request.getHeader("username");
        String username = TokenUtil.getName(token);
        System.out.println(username);

        String s = (String) redisTemplate.opsForValue().get(username);
//        String s = ToolUtil.jedis.get(username);
        System.out.println("jedis存储的token为：" + s);
        if (!TokenUtil.verify(token)) {
//            System.out.println("验证失败");
            response.getWriter().print(3);
            return false;
        }
        if (s == null) {
//            response.setHeader("content-type","text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
//            String rs = "<script style=\"color: red\" charset=\"UTF-8\" >alert(\"您已经被踢下线！\")</script>";
//            writer.println(rs);
//            writer.write(rs);
//            writer.print("<script language='javascript'>" +
//                    "alert('您已经被踢下线！');" +
//                    "window.location.href='login';</script>')");
//            System.out.println("dawdawdawd");
            writer.print(0);

            return false;
        }
//        if (true) {
//            throw new IncepterException("出现了拦截器异常", 50);
//        }

        if (token.equals(s)) {//确认是token对应的用户
            System.out.println("对了");
            return true;
        } else {//不是该用户，应该拦截
//            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
////
//            String res = "<script> alert(\"您不能重复登录\");window.open(\"/login\");</script>";
////            String rs = "<script style=\"color: red\" charset=\"UTF-8\" >alert(\"不能重复登录\")</script>";
////            writer.println(res);
//            writer.write(res);
//            response.setContentType(" text/html;charset=UTF-8");
//            PrintWriter out = response.getWriter();
//            out.flush();
//            out.println("<script>");
//            out.println("alert('此用户名已存在，请重新输入！');");
//            out.println("history.back();");
//            out.println("</script>");
//            writer.print("<script language='javascript'>" +
//                    "alert('您不能重复登录！');" +
//                    "window.location.href='login';</script>");
//            ScriptManager.RegisterStartupScript(this,this.GetType(),"test","alert('成功！');",true);
            writer.print(1);
            System.out.println("您不能重复登录");
//            writer.print(res);
//            request.getRequestDispatcher("login").forward(request, response);
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
